<?php
//include the functions.php
require "include/functions.php";
//check_session();

/*
	Generates the quiry data for current stock level
	Created by		 : Lee
	Created On		 : 29.August.2011
	Last Modification: created document
	Modified On		 : 29.August.2011
	Modified By		 : Lee
*/

//Define variables
$search_categoty='';
$supplierId='0';
//Get the Suppliers ID AND Name to Fill the combobox
$result = Combo_Data("SELECT supplier_id, name FROM supplier ORDER BY supplier_id ASC");
	
while ($row = mysql_fetch_array($result))
	{
		$supplier_name[] = $row['name'];
		$supplier_id[]	 = $row['supplier_id'];
	}
// End of getting suppliers	

// Requestings inputs from the form to php variables
if(!filter_has_var(INPUT_POST, "supplierId"))
	{}
else
	{
		$supplierId	= $_REQUEST["supplierId"];
	}
if(!filter_has_var(INPUT_POST, "category"))
	{}
else
	{
		$search_categoty= $_REQUEST["category"];
	}
?>

<html>
<head>
<title>Home Electronics - Product Listiing</title>
<link rel="stylesheet" href="include/styles.css" type="text/css">
<script src="include/functions.js"></script>
<script>window.onload=function(){
	altRows('alternatecolor');
}</script>
</head>
<body>
<div id="container" align="center">
  
	<div id="body" align="center">
		<div>
        <h1>Product Listing</h1> 
        	<fieldset style="width:94%; text-align:center">
            <legend>Search Criteria</legend>
            <form name="form1" action="product_listing.php" method="post">
            <table >
            	<tr>
                    <td>Category</td><td><input name="category" value='<?=$search_categoty?>'/></td>
                    <td>Supplier</td><td>
                    <select name="supplierId" size="1" id="supplierId">
						<? $option = "<option value='0'> - Select a Supplier - </option>\n";
							for ($i = 0; $i < count($supplier_name); $i++) 
							{
								$option .= "<option ";
								if ($supplier_id[$i]==$supplierId)
								{
									$option .= "value=\"$supplier_id[$i]\" selected>$supplier_name[$i]</option>\n";
								}
								else
								{
									$option .= "value=\"$supplier_id[$i]\">$supplier_name[$i]</option>\n";
								}
                            }
                            echo $option;
                            ?>
                    </select>
                    </td><td><input type="submit" name="search" value="Search"></td>
                </tr>
            </table>
            </form>
            </fieldset>

        	<?php
			$table = ("product p, supplier s");
			$table_rows= array ("p.product_id","p.description","p.quantity_in_stock","p.minimum_stock_level","p.category","p.product_name","s.name");
			$condition = ("(s.supplier_id = p.supplier_id) and (category like '$search_categoty%' or category='') and (p.supplier_id=$supplierId or $supplierId='0')");
		
			echo("<table class='altrowstable' id='alternatecolor'><tr><th width='4%'>ID</th><th>Description</th><th width='10%'>Quantity</th><th width='10%'>Min Stock</th><th width='12%'>Category</th><th width='12%'>Name</th><th width='10%'>Supplier</th></tr>");
			$result = query_table("home_electronics","home","home_e",$table,$table_rows,$condition, false, false);
			echo($result."<table></a>");
			?>
		
		</div>  
	</div> <!--body div -->
</div>
</body>
</html>